package com.netnovelstore.controller;

import com.netnovelstore.annotation.ResultInterceptor;
import com.netnovelstore.pojo.Book;
import com.netnovelstore.pojo.Exception.NotDataException;
import com.netnovelstore.pojo.Result;
import com.netnovelstore.service.BookService;
import com.netnovelstore.viewObject.BookListResult;
import com.netnovelstore.viewObject.ReadBookResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@ResultInterceptor
@RequestMapping("/Book")
@CrossOrigin
@Slf4j
@Api(tags = "书籍接口")
public class BookController {

    private BookService bookServiceImpl;

    @Autowired
    public BookController(BookService bookServiceImpl) {
        this.bookServiceImpl = bookServiceImpl;
    }


    @GetMapping("/getBookList")
    @ApiOperation("分页获取所有书")
    public Result<List<Book>> getBookList(@NotNull Integer pageSize, @NotNull Integer currentPage) throws Exception {

        return bookServiceImpl.getBookList(currentPage, pageSize);
    }

    @GetMapping("/countBookList")
    @ApiOperation("获取所有书的总数")
    public Result<Integer> countBookList() {
        return bookServiceImpl.countBookList();
    }

    /**
     * @return com.netnovelstore.pojo.Result
     * @Author maotao
     * @Description
     * @Date 16:38 2022/10/5
     * @Param [bookNumber] 书的编号
     * @Param [number] 第几章
     **/
    @GetMapping("/readBook/{bookNumber}/{number}")
    @ApiOperation("获取书籍正文")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "书籍编号", name = "bookNumber", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(value = "章节序号", name = "number", dataType = "Integer", paramType = "query"),
    })
    public Result<ReadBookResult> readBook(@PathVariable(name = "bookNumber") Integer bookNumber, @PathVariable(name = "number") Integer number) {
        return bookServiceImpl.readBook(bookNumber, number);
    }

    @GetMapping("/getChapterTotal")
    @ApiOperation("获取书籍章节总数")
    public Result<Integer> getChapterTotal(@NotNull Integer bookNumber) {
        return bookServiceImpl.getChapterTotal(bookNumber);
    }

    @GetMapping("/searchBook")
    @ApiOperation("按 关键词 分页搜索书")
    public Result<List<Book>> searchBook(@NotBlank String keyword, @NotNull Integer currentPage, @NotNull Integer pageSize) {
        return bookServiceImpl.searchBook(keyword, currentPage, pageSize);
    }


    @GetMapping("/getBookListByType")
    @ApiOperation("按 类型 分页搜索书")
    public Result<List<Book>> getBookListByType(@NotNull Integer pageSize, @NotNull Integer currentPage, @NotBlank String type) throws NotDataException {

        return bookServiceImpl.getBookListByType(currentPage, pageSize, type);
    }

    @GetMapping("/getCountSearchBook")
    @ApiOperation("按 关键词 获取搜索结果的数量")
    public Result<Integer> getCountSearchBook(@NotBlank String keyword) throws Exception {
        return bookServiceImpl.getCountBySearchBook(keyword);
    }

    @GetMapping("/getCountBySearchBookByType")
    @ApiOperation("按 类型 获取搜索结果的数量")
    public Result<Integer> getCountBySearchBookByType(@NotBlank String type) throws Exception {
        log.info(type);
        return bookServiceImpl.getCountBySearchBookByType(type);
    }

    @GetMapping("/getBookListByFree")
    @ApiOperation("按 免费类型 分页搜索书")
    public Result<BookListResult> getBookListByFree(@NotNull Integer currentPage, @NotNull Integer pageSize) throws NotDataException {
        return bookServiceImpl.getBookListByFree(currentPage, pageSize);
    }

    @GetMapping("/getBookListByFull")
    @ApiOperation("按 完本类型 分页搜索书")
    public Result<BookListResult> getBookListByFull(@NotNull Integer currentPage, @NotNull Integer pageSize) throws NotDataException {
        return bookServiceImpl.getBookListByFull(currentPage, pageSize);
    }

    @GetMapping("/getBookListByHot")
    @ApiOperation("按 热度 分页搜索书")
    public Result<List<Book>> getBookListByHot(@NotNull Integer currentPage, @NotNull Integer pageSize) throws NotDataException {
        return bookServiceImpl.getBookListByHot(currentPage, pageSize);
    }

    @GetMapping("/getBookListByNew")
    @ApiOperation("按 新书 分页搜索书")
    public Result<List<Book>> getBookListByNew(@NotNull Integer currentPage, @NotNull Integer pageSize) throws NotDataException {
        return bookServiceImpl.getBookListByNew(currentPage, pageSize);
    }


}
